package com.whacos.app.api;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author xiongdun
 * @description ApiController
 * @since 2018/12/12 22:36
 */
@Api(value = "app用户登录验证请求", description = "app用户登录验证请求")
@Controller
@RequestMapping(value = "/app")
public class ApiController {

    @GetMapping("/api")
    public String api() {
        return "swagger-ui";
    }


    @ApiResponse(code = 200, message = "请求成功", response = UserTokenVO.class)
    @ApiOperation(value = "APP用户登录验证", notes = "必须登录获取token才能请求其他接口", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/token")
    @ResponseBody
    public String token(@RequestBody UserTokenForm userTokenForm) {
        UserTokenVO userTokenVO = new UserTokenVO();

        return JSONObject.toJSONString(userTokenVO);
    }

    @ApiModel(value = "用户登录请求的对象", description = "userToken")
    static class UserTokenForm {

        @ApiModelProperty(value = "userName", notes = "用户登录名", name = "用户名", required = true)
        private String userName;
        @ApiModelProperty(value = "password", notes = "用户登录密码", name = "用户密码",required = true)
        private String password;

        public UserTokenForm(String userName, String password) {
            this.userName = userName;
            this.password = password;
        }

        public UserTokenForm() {
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }

    // 返回生成的token
    static class UserTokenVO {
        private String userId;
        private String userName;
        private String token;

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }
    }
}


